package com.hwua.dao.Impl;

import com.hwua.Util.C3P0Util;
import com.hwua.dao.IUserDao;
import com.hwua.entity.User;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;

public class UserDaoImpl implements IUserDao {
    private QueryRunner queryRunner=null;

    public UserDaoImpl() {
        queryRunner=new QueryRunner(C3P0Util.getCpds());
    }

    @Override
    public User query(User user) throws Exception {
        String sql="select * from amz_user where uname=? and pwd=?";
        Object[] args={user.getUname(),user.getPwd()};
        return queryRunner.query(sql,new BeanHandler<User>(User.class),args);
    }

    @Override
    public User query(String name) throws Exception {
        String sql="select * from amz_user where uname=?";
        Object[] args={name};
        return queryRunner.query(sql,new BeanHandler<User>(User.class),args);
    }

    @Override
    public User query(String name, String email) throws Exception {
        String sql="select * from amz_user where uname=? and email=?";
        Object[] args={name,email};
        return queryRunner.query(sql,new BeanHandler<User>(User.class),args);
    }

    @Override
    public int insert(User user) throws Exception {
        String sql="insert into amz_user values(null,?,?,?,?,?,?,?,?,?)";
        Object[] args={user.getUname(),user.getPwd(),user.getSex(),user.getBirthday(),user.getIdcard(),user.getEmail(),user.getMobile(),user.getAddress(),user.getUtype()};
        return queryRunner.update(sql,args);
    }

    @Override
    public int update(User user) throws Exception {
        String sql="update amz_user set pwd=?";
        Object[] args={user.getPwd()};
        return queryRunner.update(sql,args);
    }
}
